#include "Common.h"
#include "Util.h"


std::vector<std::string> SplitString(const std::string &src, const std::string &sep)
{
	std::vector<std::string> r;
	std::string s;
	for (std::string::const_iterator i = src.begin(); i != src.end(); i++) {
		if (sep.find(*i) != std::string::npos) {
			if (s.length()) 
				r.push_back(s);
			s = "";
		} else {
			s += *i;
		}
	}
	if (s.length()) r.push_back(s);
	return r;
}

std::vector<uint32> SplitStringToUINT(const std::string &src, const std::string &sep)
{
	std::vector<uint32> r;
	std::string s;
	for (std::string::const_iterator i = src.begin(); i != src.end(); i++) {
		if (sep.find(*i) != std::string::npos) {
			if (s.length()) r.push_back(atol(s.c_str()) );
			s = "";
		} else {
			s += *i;
		}
	}
	if (s.length()) r.push_back( atol(s.c_str()) );
	return r;
}


bool parseDataFromString(const char* data, char seperator, int arrayLength, int* dataArray)
{

	const char* start = data;
	const char* end = NULL;
	int count = 0;
	while(count < arrayLength)
	{
		end = strchr(start, seperator);
		if (!end && !strlen(start))
			break;
		if (!end)
			end = start + (strlen(start) - 1);
		dataArray[count] = (uint32)atol(start);
		start = end + 1;
		count++;
	}
	////test last one
	//if (strlen(start) > 0 && count == arrayLength - 1)
	//{
	//	dataArray[count] = (uint32)atol(start);
	//}

	return true;

}

std::string GetWord(const char*& pString)
{
	std::string strWord;
	while (*pString != ' ' && *pString != '\0')
	{
		strWord += *pString;
		pString++;
	}
	while (*pString == ' ') pString++; 
	return strWord;
}

std::string BYTE2HEX(const char* buffer,int nSize)
{
	char buf[1024];
	memset(buf,0,1024);
	int len = 0;
	for(int i = 0;i < nSize;i++)
	{
		int tmp = sprintf(buf + len,"%02x ",buffer[i]);
		len += tmp;
	}
	std::string str(buf);
	return str;
}